package Yang.homework3;

import java.util.*;

/**
 * (1)定义一个学生类Student，包含属性：姓名(String name)、年龄(int age)
 * (2)定义Map集合，用Student对象作为key，用字符串(此表示表示学生的住址)作为value
 * (3)利用四种方式遍历Map集合中的内容
 */
public class StudentTest {
    private String name;
    private int age;

    public StudentTest() {
    }

    public StudentTest(String name, int age) {
        setName(name);
        setAge(age);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        StudentTest that = (StudentTest) o;
        return age == that.age &&
                Objects.equals(name, that.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "StudentTest{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public static void main(String[] args) {
        Map<StudentTest,String> student = new HashMap<>();
        student.put(new StudentTest("张三", 18), "北京");
        student.put(new StudentTest("李四", 19), "上海");
        student.put(new StudentTest("老王", 20), "南京");
        System.out.println("student = " + student);

        //利用四种方式遍历Map集合中的内容，方式一：利用keySet()
        Set<StudentTest> studentTests = student.keySet();
        for(StudentTest stu : studentTests){
            System.out.println(stu +"来自于" + student.get(stu));
        }
        System.out.println("----------------------------------------");
        //利用四种方式遍历Map集合中的内容，方式二：利用迭代器
        Iterator<StudentTest> iterator = studentTests.iterator();
        while(iterator.hasNext()){
            StudentTest next = iterator.next();
            System.out.println(next + "来自于" + student.get(next));
        }
        System.out.println("----------------------------------------");
        //利用四种方式遍历Map集合中的内容，方式三：利用entrySet()
        Set<Map.Entry<StudentTest, String>> entries = student.entrySet();
        for (Map.Entry<StudentTest, String> stu1 : entries){
            System.out.println(stu1.getKey() + "来自于" + stu1.getValue());
        }

        System.out.println("----------------------------------------");

        //利用四种方式遍历Map集合中的内容，方式四：利用迭代器
        Iterator<Map.Entry<StudentTest, String>> iterator1 = entries.iterator();
        while(iterator1.hasNext()){
            Map.Entry<StudentTest, String> next = iterator1.next();
            System.out.println(next);
        }
    }
}
